package org.wak.scene
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import org.wak.action.actions.AdventureActions;
	import org.wak.baseoneonline.flash.geom.IntPoint;
	import org.wak.main.AdventureMaster;

	/**
	 * This class represents an interactable Item inside a Scene. 
	 * 
	 */	
	public class SceneItem extends Sprite implements ISceneInteractable
	{
		// the id of this item.
		private var mItemId:String = "" ;
		
		// Name of this item
		private var mName:String = "" ;
		
		// Description of this item
		private var mDescription:String = "" ;
		
		// Point where the player will walk to interact.
		private var mInteractionPoint:IntPoint = null ;
		
		// anim behaviour that the player will have after walk.
		private var mInteractionPointBehaviour:String = "" ;
		
		// The graphic or anim to show on the Scene.
		private var mCurrentItemDisplay:DisplayObject = null ;
		
		// Asoc array of views(DisplayObjects) by state id.
		private var mViewStates:Array = null
		
		// Horizontal line to draw this item back or front.
		private var mBaseline:int = 0;
		
		private var mUnitaryUse:Boolean = true ;
		
		// current scene id 
		private var mSceneId:String = "" ;
		
		// the default action for this object.
		private var mDefaultAction:String = AdventureActions.ActionIdLookAt ;
		
		private var mEnabled:Boolean = true ;
		
		public function SceneItem(pDef:ISceneItemDefinition)
		{
			super();
			
			mItemId = pDef.itemId ;
			mUnitaryUse = pDef.unitaryUse ;
			mDescription = pDef.description ;
			
			if(pDef.defaultAction != "")
				mDefaultAction = pDef.defaultAction ;
				
			mInteractionPoint = pDef.interactionPoint ;
			
			//mCurrentItemDisplay = new AdventureMaster.Resources[ pDef.displayObjectId ] as DisplayObject;
			mViewStates = new Array();
			var fView:DisplayObject = null ;
			for each( var view:Object in pDef.viewStates)
			{
				fView = new AdventureMaster.Resources[ view.res ] as DisplayObject;
				mViewStates[view.state] = fView ;
				if( view.state == "default")
					mCurrentItemDisplay = fView ;
			}
			
			this.addChild(mCurrentItemDisplay);
			
			mBaseline = pDef.baseline ;
			
			mName = pDef.itemName ;
			
			mEnabled = pDef.enabled ;
			
			mCurrentItemDisplay.visible = pDef.visible ;
			
			this.x = pDef.startingPoint.x ;
			this.y = pDef.startingPoint.y ;
			
			// set default baseline
			if( mBaseline == -1)
			{
				mBaseline = this.x + mCurrentItemDisplay.height ;
			}
			mInteractionPointBehaviour = pDef.interactionBehaviour ;
			
			 
		}
		
		/**
		 * Change the view state of the Scene item. 
		 * @param pViewId id of the new state.
		 * 
		 */		
		public function SetView( pViewId:String ):void
		{
			if( mViewStates[pViewId] == null )
				throw new Error("There is no "+pViewId+" state for "+ mItemId);
				
			if(	this.contains(mCurrentItemDisplay) )
				removeChild(mCurrentItemDisplay);
				
			mCurrentItemDisplay = mViewStates[pViewId] ;
			addChild(mCurrentItemDisplay);
		}
		
		public function get enabled():Boolean 
		{
			return mEnabled ;
		}
		
		public override function get visible():Boolean
		{
			return mCurrentItemDisplay.visible ;
		}
		
		public override function set visible(value:Boolean):void
		{
			super.visible = value ;
			mCurrentItemDisplay.visible = value ;
		}
		
		public override function toString():String
		{
			return mItemId + " bl:"+ mBaseline ;
		}
		
		
		public function get interactableId():String
		{
			return mItemId ;	
		}
		
		public function get interactionName():String
		{
			return mName ;
		}
		
		public function set interactionName(value:String):void
		{
			mName = value;
		}
		
		public function get displayObject():DisplayObject 
		{
			return this as Sprite ;
		}
		
		public function get baseline():int
		{
			return mBaseline ;
		}
		
		public function set baseline(value:int):void
		{
			mBaseline = value ;
		}
		
		public function get interactionPointX():Number
		{
			return mInteractionPoint.x ;
		}
		
		public function set interactionPointX(value:Number):void
		{
			mInteractionPoint.x = value;
		}
		
		public function get interactionPointY():Number
		{
			return mInteractionPoint.y ;
		}
		
		public function set interactionPointY(value:Number):void
		{
			mInteractionPoint.y = value;
		}		
		
		public function get interactionBehaviour():String 
		{
			return mInteractionPointBehaviour ;
		}
		
		public function set interactionBehaviour(value:String):void 
		{
			mInteractionPointBehaviour = value;
		}
		
		public function get unitaryUse():Boolean 
		{
			return mUnitaryUse ;	
		}
		
		public function set unitaryUse(value:Boolean):void 
		{
			mUnitaryUse = value;	
		}
		
		public function get description():String 
		{
			return mDescription ;
		}
		
		public function set description(value:String):void 
		{
			mDescription = value;
		}
		
		public function get sceneId():String
		{
			return mSceneId ;
		}
		
		public function set sceneId(value:String ):void
		{
			mSceneId = value;
		}
		
		public function get defaultAction():String 
		{
			return mDefaultAction ;
		}
		
		public function set defaultAction(value:String):void 
		{
			mDefaultAction = value;
		}
		
		
	}
}